using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InputCtrl : MonoBehaviour
{

    private int ropeItemColliderMask;
    private TrailRenderer trailRenderer;

    // Start is called before the first frame update
    void Start()
    {
        trailRenderer = GetComponent<TrailRenderer>();
        ropeItemColliderMask = LayerMask.GetMask(LayerNames.RopeItemCollider);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            var point = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
            var worldPoint = Camera.main.ScreenToWorldPoint(point);
            transform.position = worldPoint;
        }

        if (Input.GetMouseButtonUp(0))
        {
            trailRenderer.Clear();
        }

        if (Input.GetMouseButton(0))
        {
            var point = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1);
            var worldPoint = Camera.main.ScreenToWorldPoint(point);
            trailRenderer.AddPosition(worldPoint);


            // 判断是否碰到绳子
            // 先只切割一个绳子
            Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
            RaycastHit hitInfo;
            // // Physics.Raycast(ray, out hitInfo, Mathf.Infinity, ropeMask)
            if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, ropeItemColliderMask))
            {
                // 删除 一个 item
                GameObject obj = hitInfo.collider.gameObject;
                Debug.Log($"发生了碰撞.....:{obj.name}");
                var rope3 = obj.GetComponentInParent<Rope3>();
                if(rope3 != null)
                {
                    rope3.RemoveRopeItem(obj.transform.parent);
                }
            }
        }
    }
}
